home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Universität Tübingen - 1997/1998 Winter
/
Universität Tübingen - Wintersemester 1997-98 - Uni-Informationssystem und Stadt-Informationssystem.iso
/
bcm
/
cell2.jav
< prev
next >
Wrap
Text File
|
1997-08-14
|
9KB
|
369 lines
// ----------------------------------------------------------------------------
// $Header: D:/users/moroff/java/cell2/RCS/cell2.java 1.5 1996/12/11 20:30:16 moroff Exp moroff $
// $Source: D:/users/moroff/java/cell2/RCS/cell2.java $
// $Revision: 1.5 $
// $Name: $
// $Date: 1996/12/11 20:30:16 $
// $Locker: moroff $
// ----------------------------------------------------------------------------
// Cellular Automata Applet
// ----------------------------------------------------------------------------
import java.applet.*;
import java.awt.*;
// Cellular Automata Class
public class cell2 extends Applet implements Runnable
{
// Declare Variables
int arraySize = 20,
cellHeight,
cellWidth;
byte cells[][],
next[][];
boolean initPaint = true;
int waitTime = 50;
int threshHold = 2;
byte immStates = 1,
infStates = 1;
boolean threadRunning = false;
Thread ttapeThread = null;
Image im;
Graphics osGraphics;
// Controls
Panel pnlCtrl = new Panel ();
Button btnStart = new Button ("Start"),
btnStop = new Button ("Stop"),
btnCont = new Button ("Continue");
Label lblThreshhold = new Label ();
Label lblImmStates = new Label (),
lblInfStates = new Label ();
/** Initialize Applet, ▄berladung der init()-Methode von java.applet.Applet */
public void init(){
// Gr÷▀e der Zellen berechnen
// Graphik-Buffer erzeugen
im=createImage(size().height, size().height);
osGraphics = im.getGraphics();
// Buttons hinzufⁿgen
pnlCtrl.setLayout (new GridLayout(16,1));
pnlCtrl.setBackground (Color.lightGray);
pnlCtrl.add (btnStart);
pnlCtrl.add (btnStop);
pnlCtrl.add (new Label(""));
pnlCtrl.add (new Button ("1"));
pnlCtrl.add (new Button ("2"));
pnlCtrl.add (new Button ("3"));
pnlCtrl.add (new Button ("4"));
pnlCtrl.add (new Button ("5"));
pnlCtrl.add (new Button ("6"));
pnlCtrl.add (new Button ("7"));
pnlCtrl.add (lblThreshhold);
pnlCtrl.add (lblImmStates);
pnlCtrl.add (lblInfStates);
setLayout (new BorderLayout());
add ("East", pnlCtrl);
// Buttons disablen
btnStop.disable();
btnCont.disable();
//
initCells(1);
}
/** Override Applet Class' paint method */
public synchronized void paint(Graphics g){
paintCells(osGraphics);
g.drawImage(im, 0, 0, null);
}
/** Initiales Gitter setzen */
private void initCells(int mode) {
arraySize = 40;
cells = null;
next = null;
cells = new byte [arraySize][arraySize];
next = new byte [arraySize][arraySize];
cellHeight = size().height / arraySize;
cellWidth = size().height / arraySize;
for ( int line = 0; line < arraySize; ++line ) {
for ( int col = 0; col < arraySize; ++col )
cells[col][line] = next[col][line] = 0;
}
switch (mode) {
case 1:
waitTime = 60;
threshHold = 1;
immStates = 1;
infStates = 1;
cells[17][17] = 1;
cells[17][18] = 2;
cells[21][21] = 2;
cells[21][22] = 1;
cells[24][20] = 2;
cells[25][20] = 1;
break;
case 2:
threshHold = 3;
immStates = 2;
infStates = 4;
cells[18][19] =
cells[18][20] =
cells[19][19] =
cells[19][20] =
cells[20][19] =
cells[20][20] = 1;
break;
case 3:
threshHold = 3;
immStates = 2;
infStates = 4;
cells[18][19] =
cells[18][20] =
cells[18][21] =
cells[19][19] =
cells[19][20] =
cells[19][21] =
cells[20][19] =
cells[20][20] =
cells[20][21] = 1;
break;
case 4:
threshHold = 2;
immStates = 1;
infStates = 1;
cells[19][17] =
cells[18][18] =
cells[17][19] =
cells[20][22] =
cells[21][21] =
cells[22][20] =1;
break;
case 5:
threshHold = 3;
immStates = 5;
infStates = 5;
cells[23][15] = 9;
cells[24][15] = 8;
cells[25][15] = 3;
cells[26][15] = 1;
cells[22][16] = 10;
cells[23][16] = 8;
cells[24][16] = 7;
cells[25][16] = 4;
cells[26][16] = 2;
cells[22][17] = 7;
cells[23][17] = 6;
cells[24][17] = 5;
cells[25][17] = 3;
cells[26][17] = 1;
cells[22][18] = 3;
cells[23][18] = 4;
cells[24][18] = 3;
cells[25][18] = 2;
cells[22][19] = 1;
cells[23][19] = 2;
cells[24][19] = 1;
break;
case 6:
threshHold = 2;
immStates = 3;
infStates = 2;
cells[17][19] = 3;
cells[18][19] = 4;
cells[19][19] = 3;
cells[17][20] = 2;
cells[18][20] = 1;
cells[19][20] = 2;
break;
case 7:
threshHold = 2;
immStates = 3;
infStates = 2;
cells[17][19] = 4;
cells[18][19] = 5;
cells[19][19] = 4;
cells[17][20] = 1;
cells[18][20] = 2;
cells[19][20] = 1;
break;
} // switch
lblThreshhold.setText ("s = "+Integer.toString(threshHold));
lblImmStates .setText ("g = "+Integer.toString(immStates));
lblInfStates .setText ("a = "+Integer.toString(infStates));
initPaint = true;
}
private boolean isHealthy (byte cell) {
return cell == 0;
}
private boolean isInfectious (byte cell) {
return cell > 0 && cell <= infStates;
}
private boolean isImmune (byte cell) {
return cell > infStates;
}
private byte nextState (byte cell) {
return ( cell < infStates + immStates ) ? ++cell : (byte)0;
}
private void calculateCells (){
for ( int line = 1; line < arraySize - 1; ++line ) {
for ( int col = 1; col < arraySize - 1; ++col ) {
if ( !isHealthy(cells[col][line]) )
next[col][line] = nextState(cells[col][line]);
else if ( calculateNeighbours(col,line) >= threshHold )
next[col][line] = 1;
}
}
for ( int line = 0; line < arraySize; ++line )
for ( int col = 0; col < arraySize; ++col )
cells[col][line] = next [col][line];
}
private int calculateNeighbours (int col, int line){
int sum = 0;
for ( int i = line - 1; i <= line + 1; ++i )
for ( int j = col - 1; j <= col + 1; ++j )
if ( isInfectious (cells[j][i]) )
++sum;
return sum;
}
private void paintCells (Graphics g){
if ( initPaint ) {
g.setColor(Color.white);
g.fillRect(0, 0, arraySize * cellWidth, arraySize * cellHeight);
initPaint = false;
}
for ( int line = 0; line < arraySize; ++line ) {
for ( int col = 0; col < arraySize; ++col ) {
byte cell = cells[col][line];
cell = cells[col][line];
if ( isInfectious (cell) )
g.setColor(new Color(255 - 128 * (cell - 1) / infStates,0,0));
else if ( isHealthy (cell) )
g.setColor(Color.white);
else {
int c = 124 + 100 * (cell - infStates - 1) / immStates;
g.setColor(new Color(c,c,c));
}
g.fillRect(col * cellWidth, line * cellHeight, cellWidth, cellHeight);
}
}
}
synchronized void calcNPaint()
{
calculateCells();
repaint();
}
// Change coordinates and repaint
public void run(){
while(ttapeThread != null){
try {
Thread.sleep (waitTime);
}
catch (InterruptedException e) {
}
calcNPaint();
}
}
// Re-paint when buffer is updated
public void update(Graphics g) {
paint(g);
}
// Handle mouse clicks
public boolean handleEvent(Event evt) {
// Button-Events behandeln
if ( evt.target instanceof Button ) {
try {
Integer buttonNumber = new Integer ((String)evt.arg);
if ( buttonNumber.intValue() >= 1 && buttonNumber.intValue() <= 9 ) {
if ( ttapeThread != null ) {
ttapeThread.stop();
ttapeThread = null;
}
initCells(buttonNumber.intValue());
repaint();
showStatus ("Press Start to run the automata");
}
}
// Button nicht numerisch
catch (NumberFormatException e) {
if ( (String)evt.arg == "Start" ) {
ttapeThread = new Thread(this);
ttapeThread.start();
threadRunning = true;
}
else if ( (String)evt.arg == "Stop" ) {
ttapeThread.stop();
ttapeThread = null;
threadRunning = false;
}
}
// Stop/Continue-Buttons de-/aktivieren
if ( ttapeThread == null ) {
btnStart.enable();
btnStop .disable();
}
else {
btnStart.disable();
btnStop .enable();
}
return true;
}
else
return super.handleEvent(evt);
}
// Stop thread then clean up before close
public void stop(){
if(ttapeThread != null)
ttapeThread.stop();
ttapeThread = null;
}
} // End TickerTape
// ----------------------------------------------------------------------------
// $Log: cell2.java $
// Revision 1.5 1996/12/11 20:30:16 moroff
// Experimentieren mit Thread-Kontrolle
//
// Revision 1.4 1996/11/26 22:46:44 moroff
// Farbumsetzung
//
// Revision 1.3 1996/11/26 21:50:31 moroff
// Mehrere Beispiele
//
// Revision 1.2 1996/11/22 23:45:50 moroff
// Kosmetik an den RCS-Keywords
//
// Revision 1.1 1996/11/22 23:44:26 moroff
// Initial revision
//
// ----------------------------------------------------------------------------